/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <gsystems/gsConnection.h>
#include <netdb.h>

gsConnection::gsConnection() :
m_state( GS_CONNECTION_UNINITIALIZED )
{
	m_socket = new gsSocket();
}

gsConnection::gsConnection( const gsAddress address ) :
m_endPoint( address ),
m_state( GS_CONNECTION_ENDPOINT_SET )
{
	m_socket = new gsSocket();
}

void gsConnection::setAddress( const gsAddress address )
{
	if( m_state == GS_CONNECTION_CONNECTED )
	{
		breakConnection();
	}
	
	m_endPoint = address;
	m_state = GS_CONNECTION_ENDPOINT_SET;
}

gsBool gsConnection::establishConnection()
{	
	if( m_state >= GS_CONNECTION_ENDPOINT_SET )
	{
		return m_socket->establishConnection( m_endPoint );
	}
	else
	{
		return false;
	}
}

const gsInt gsConnection::sendData( const void* dataBuffer, const gsUInt bufferSize )
{
	return m_socket->sendData( dataBuffer, bufferSize );
}
	
const gsInt gsConnection::receiveData( void* dataBuffer, const gsUInt bufferSize )
{
	return m_socket->receiveData( dataBuffer, bufferSize );
}

gsInt gsConnection::getHandle()
{
	if( m_socket )
	{
		return m_socket->getSocketHandle();
	}
	else
	{
		return GS_NULL;
	}
}

void gsConnection::breakConnection()
{
	m_state = GS_CONNECTION_DISCONNECTED;
	m_socket->breakConnection();
}

gsConnection::~gsConnection()
{
	delete m_socket;
}


